﻿using aier.business.logic;
using aier.dataaccess.model;
using aier.dataaccess.dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using log4net;

namespace aier.wx.api.selfhost.Controllers
{
    /// <summary>
    /// 班级
    /// </summary>
    [Authorize]
    [RoutePrefix("api/Class")]
    public class ClassController : ApiController
    {
        /// <summary>
        /// 班级逻辑
        /// </summary>
        public ClassBll ClassInfoBll { get; set; }
        /// <summary>
        /// 日志
        /// </summary>
        public ILog log { get; set; }

        public ApiTool apiTool { get; set; }

        /// <summary>
        /// 获取所有的班级信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetAllClass")]
        public IHttpActionResult GetAllClass()
        {
            List<ClassView> classs = ClassInfoBll.QueryAllClass();

            return Ok<List<ClassView>>(classs);
        }

        /// <summary>
        /// 通过ID获取班级信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetClassById")]
        public IHttpActionResult GetClassById(string classId)
        {
            ClassView ClassView = ClassInfoBll.QueryClassById(classId);

            return Ok<ClassView>(ClassView);
        }

        /// <summary>
        /// 通过名称获取班级信息
        /// 情景：若搜索前选择了学段或学校，则添加学段或学校条件查找
        /// </summary>
        /// <param name="province">省名称</param>
        /// <param name="city">市名称</param>
        /// <param name="area">区名称</param>
        /// <param name="className">班级名称</param>
        /// <param name="stage">学段(可空)</param>
        /// <param name="schoolid">学校ID(可空)</param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetClassByName")]
        public IHttpActionResult GetClassByName(string province, string city, string area,string className, int stage = 0, Guid? schoolid = null)
        {
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            List<ClassView> ClassViews = ClassInfoBll.QueryClassByName(province, city, area, className, userInfo.siteid.ToString(), stage, schoolid);

            return Ok<List<ClassView>>(ClassViews);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetClassByProviceCityCounty")]
        public IHttpActionResult GetClassByProviceCityCounty(string province, string city, string area)
        {
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            List<ClassView> ClassViews = ClassInfoBll.QueryClassByProviceCityCounty(province, city, area, userInfo.siteid.ToString());

            return Ok<List<ClassView>>(ClassViews);
        }
        /// <summary>
        /// 根据省市区县名称，学段(可空)，学校id(可空)查询班级视图信息
        /// </summary>
        /// <param name="province">省</param>
        /// <param name="city">市</param>
        /// <param name="area">区</param>
        /// <param name="stage">学段</param>
        /// <param name="schoolid">学校ID</param>
        /// <returns></returns>
        [HttpGet]
        [Route("QueryClassByPCAandStageSchool")]
        public IHttpActionResult QueryClassByPCAandStageSchool(string province, string city, string area, int stage = 0,Guid?schoolid=null)
        {
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            List<ClassView> ClassViews = ClassInfoBll.QueryClassByPCAandStage(province, city, area, userInfo.siteid.ToString(),stage, schoolid);

            return Ok<List<ClassView>>(ClassViews);
        }

        /// <summary>
        /// 班级列表查询接口实现 getClassList
        /// </summary>
        /// <param name="classinfo">班级信息</param>
        /// <param name="hospitalId">医院ID</param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetClassListByConditions")]
        public IHttpActionResult getClassListByConditions(ClassView classinfo, Guid hospitalId)
        {
            try
            {
                if (classinfo != null)
                {
                    List<ClassView> classViews = ClassInfoBll.getClassListByConditions(classinfo, hospitalId);
                    return Ok<List<ClassView>>(classViews);
                }
                return NotFound();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }
        /// <summary>
        /// 创建班级 保存班级接口实现 
        /// 原程序 ScClassServiceImpl/saveClass
        /// </summary>
        /// <param name="classInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult Put(ClassInfo classInfo)
        {
            return Ok<ResResult>(ClassInfoBll.CreateClass(classInfo));
        }

        /// <summary>
        /// 修改班级 更新班级接口实现 updateClass
        /// </summary>
        /// <param name="classInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult Post(ClassInfo classInfo)
        {
            ResResult resResult =  ClassInfoBll.UpdateClassById(classInfo);
            return Ok(resResult);
        }

        /// <summary>
        /// 根据学校ID或班级名称查询视图信息
        /// 原程序 ：creeningServiceImpl-> queryClass(String schoolId, String className)
        /// </summary>
        /// <param name="schoolId">学校ID</param>
        /// <param name="classname">班级名称</param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("QueryClassBySchoolIdAndClassName")]
        public IHttpActionResult QueryClassBySchoolIdAndClassName(Guid schoolId, string classname)
        {
            try
            {
                var classViews = ClassInfoBll.QueryClassBySchoolIdAndClassName(schoolId, classname);
                return Ok<IEnumerable<ClassView>>(classViews);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }
    }
}
